import scrapy
from scrapy import FormRequest


class DjangoSpider(scrapy.Spider):
    name = 'django'
    start_urls = ['http://127.0.0.1:8000/admin/login/']
    question_list_url = 'http://127.0.0.1:8000/admin/polls/question/'
    allowed_domains = ['127.0.0.1']

    def parse(self, response):
        yield FormRequest.from_response(
            response,
            url=response.url,
            formdata={'username': 'admin', 'password': 'abcd/1234'},
            callback=self.after_login
        )

    def after_login(self, response):
        # 这个函数里面做登录成功与否的判断
        if 'Log in' not in response.css('title::text').extract_first():
            print('success')
            yield scrapy.Request(
                    self.question_list_url,
                    callback=self.parse_question_list
                    )
        else:
            self.log('login failed')

    def parse_question_list(self, response):
        # for href in response.xpath('//tr//a/@href').extract():
        #     href = response.urljoin(href)
        #     yield scrapy.Request(href, callback=self.extract_data)

        for a in response.xpath('//tr//a'):
            yield response.follow(a, callback=self.extract_data)

    def extract_data(self, response):
        text = response.xpath('//input[@name="question_text"]/@value').extract_first()
        date = response.xpath('//input[@name="pub_date_0"]/@value').extract_first()
        time = response.xpath('//input[@name="pub_date_1"]/@value').extract_first()
        yield dict(text=text, date=date, time=time)
